package v2;

import beans.BeanDefinition;
import beans.factorys.PropertyValue;
import beans.factorys.config.RunTimeBeanReference;
import beans.factorys.support.DefaultBeanFactory;
import beans.factorys.xml.XmlBeanDefinitionReader;
import core.io.ClassPathResource;
import core.io.Resource;
import org.junit.Test;

import static org.junit.Assert.*;

import java.util.List;

/**
 * Created by ZhaiGuaili on 2018/7/20.
 */
public class BeanDefinitionTestV2 {

    @Test
    public void testGetBeanDefinition() {
        DefaultBeanFactory factory = new DefaultBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
        Resource resource = new ClassPathResource("petstore-v2.xml");
        reader.loadBeanDefinition(resource);

        BeanDefinition definition = factory.getBeanDefinition("petStore2");
        List<PropertyValue> list = definition.getPropertyValues();
        assertTrue(list.size() == 3);

        {
            PropertyValue pv = getPropertyValue("accountDao", list);
            assertNotNull(pv);
            assertTrue(pv.getValue() instanceof RunTimeBeanReference);

        }

        {
            PropertyValue pv = getPropertyValue("itemDao", list);
            assertNotNull(pv);
            assertTrue(pv.getValue() instanceof RunTimeBeanReference);
        }

    }

    private PropertyValue getPropertyValue(String name, List<PropertyValue> list) {
       assertNotNull("name is null",name);
       assertNotNull("PropertyValue list is null",list);
        for(PropertyValue property : list){
            if(name.equals(property.getName())){
                return property;
            }

        }
        return null;
    }
}
